package com.enjoyor.soa.traffic.frame.support.xmpp;

import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.springframework.util.StringUtils;

public class SmackConnection {

    private static final String XMPP_HOST = "192.168.91.168";

    private static final String SERVICE_NAME = "jt5-168";

    private static final String LOGIN_NAME = "admin";

    private static final String LOGIN_PASSWORD = "12345";

    private static SmackConnection smackConnection = null;

    private XMPPTCPConnection connection = null;

    private synchronized static SmackConnection getInstance() {
        if (smackConnection == null)
            smackConnection = new SmackConnection();
        return smackConnection;
    }

    public XMPPTCPConnection getConnection() {
        if (!isAuthenticated()) {
            openConnection();
        }
        return connection;
    }

    public boolean sendSmackMessage(String user, String message) {

        if (StringUtils.isEmpty(user))
            return false;
        try {
            connection = getConnection();
            if (connection.isAuthenticated()) {
                ChatManager chatManager = ChatManager.getInstanceFor(connection);
                Chat chat = chatManager.createChat(user + "@" + SERVICE_NAME + "/Smack", null);
                chat.sendMessage(message);

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;

    }

    @SuppressWarnings("unused")
    private void closeConnection() {
        if (connection != null)
            if (connection.isConnected())
                connection.disconnect();
    }

    /**
     * 判断连接是否通过了身份验证 即是否已登录
     *
     * @return
     */
    public boolean isAuthenticated() {
        return connection != null && connection.isConnected() && connection.isAuthenticated();
    }

    private void openConnection() {
        try {
            if (null == connection || !connection.isAuthenticated()) {
                XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
                configBuilder.setHost(XMPP_HOST);
                configBuilder.setServiceName(SERVICE_NAME);
                configBuilder.setUsernameAndPassword(LOGIN_NAME, LOGIN_PASSWORD);
                configBuilder.setSecurityMode(SecurityMode.disabled);
                connection = new XMPPTCPConnection(configBuilder.build());
                connection.connect();
                connection.login();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        try {

            SmackConnection smackConnection = SmackConnection.getInstance();

            //XMPPTCPConnection connection = SmackConnection.getInstance().getConnection();

            String message = "{\"theme\":\"ejcds\",\"time\":\"2018-11-10 14:00:55\",\"type\":\"警情反馈\",\"title\":\"测试88\"}";

            smackConnection.sendSmackMessage("051338", message);

            /*connection.addAsyncStanzaListener(new StanzaListener() {

                @Override
                public void processPacket(Stanza packet) throws NotConnectedException {
                    System.out.println((new Date()).toString() + "- processPacket:" + packet.toXML());
                }
            }, new StanzaFilter() {
                @Override
                public boolean accept(Stanza stanza) {
                    // System.out.println(stanza.toString());
                    return true;
                }
            });

            while (true) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }*/

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
